package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author Fxz
 * @version 1.0
 * @date 2022-02-21 16:26
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PaymentServiceImpl implements PaymentService {

	private final RabbitService rabbitService;

	private final PaymentInfoMapper paymentInfoMapper;

	/**
	 * 保存交易记录的接口
	 */
	@Override
	public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
		Long count = paymentInfoMapper.selectCount(Wrappers.<PaymentInfo>lambdaQuery()
				.eq(PaymentInfo::getOrderId, orderInfo.getImgUrl()).eq(PaymentInfo::getPaymentType, paymentType));
		if (count == 0) {
			PaymentInfo paymentInfo = new PaymentInfo();
			paymentInfo.setCreateTime(new Date());
			paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
			paymentInfo.setOrderId(orderInfo.getId());
			paymentInfo.setPaymentType(paymentType);
			paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
			paymentInfo.setSubject(orderInfo.getTradeBody());
			paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
			paymentInfoMapper.insert(paymentInfo);
		}
	}

	/**
	 * 支付成功
	 */
	@Override
	public void paySuccess(String outTradeNo, String name, Map<String, String> paramsMap) {
		// 交易记录如果是CLOSED,PAID,则直接返回不需要继续处理
		PaymentInfo info = getPaymentInfo(outTradeNo, name);
		if ("CLOSED".equals(info.getPaymentStatus()) || "PAID".equals(info.getPaymentStatus())) {
			return;
		}

		PaymentInfo paymentInfo = new PaymentInfo();
		paymentInfo.setTradeNo(paramsMap.get("trade_no"));
		paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
		paymentInfo.setCallbackTime(new Date());
		paymentInfo.setCallbackContent(paymentInfo.toString());
		updatePaymentInfo(outTradeNo, name, paymentInfo);

		// 发送消息给订单
		log.info("支付成功,发送消息给mq,更新订单状态:{}", info.getOrderId());
		rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY, MqConst.ROUTING_PAYMENT_PAY, info.getOrderId());
	}

	/**
	 * 跟据商户订单号 查询记录
	 */
	@Override
	public PaymentInfo getPaymentInfo(String outTradeNo, String name) {
		return paymentInfoMapper.selectOne(Wrappers.<PaymentInfo>lambdaQuery()
				.eq(PaymentInfo::getOutTradeNo, outTradeNo).eq(PaymentInfo::getPaymentType, name));
	}

	/**
	 * 更新交易记录
	 */
	@Override
	public void updatePaymentInfo(String outTradeNo, String name, PaymentInfo paymentInfo) {
		paymentInfoMapper.update(paymentInfo, Wrappers.<PaymentInfo>lambdaUpdate()
				.eq(PaymentInfo::getOutTradeNo, outTradeNo).eq(PaymentInfo::getPaymentType, name));
	}

	/**
	 * 关闭交易记录
	 */
	@Override
	public void closePayment(Long orderId) {
		PaymentInfo paymentInfo = new PaymentInfo();
		paymentInfo.setPaymentStatus(PaymentStatus.ClOSED.name());
		paymentInfoMapper.update(paymentInfo,
				Wrappers.<PaymentInfo>lambdaUpdate().eq(PaymentInfo::getOrderId, orderId));
	}

}
